package org.jeecg.modules.yw.controller;

import java.util.List;

import javax.annotation.Resource;

import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.yw.entity.ContractInfo;
import org.jeecg.modules.yw.entity.EnterPrise;
import org.jeecg.modules.yw.service.ContractInfoService;
import org.jeecg.modules.yw.service.EnterPriseService;
import org.jeecg.modules.yw.utils.CommonUtils;
import org.jeecg.modules.yw.vo.ContractInfoVo;
import org.jeecg.modules.yw.vo.EnterPriseVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import io.micrometer.core.instrument.util.StringUtils;
import io.swagger.annotations.Api;

@RestController
@Api(tags = "合同管理")
@RequestMapping("/contractInfos")
public class ContractInfoVController {

    @Resource
    private ContractInfoService contractInfoService;
    @Autowired
    private ISysBaseAPI sysBaseAPI;
    @Resource
    private EnterPriseService enterPriseService;
    @GetMapping("/page")
    public  org.jeecg.common.api.vo.Result page(Page<ContractInfo> page,
                                                @RequestParam(name = "name",required = false)String name,
                                                @RequestParam(name = "phone",required = false)String phone,
                                                @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize ) {
        page.setCurrent(pageNo);
        page.setSize(pageSize);
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        List<String> roleIds = sysBaseAPI.getRoleIdsByUsername(sysUser.getUsername());
        String[] areas=null;
        if (!roleIds.contains(CommonUtils.ADMIN)) {
            String area=sysUser.getArea();
            if(StringUtils.isNotBlank(area)){
                areas=area.split(",");
            }else{
                IPage<ContractInfoVo> result =new Page<>();
                return org.jeecg.common.api.vo.Result.ok(result);
            }
        }
        IPage<ContractInfoVo> result = contractInfoService.pageInfo(page,name,phone,areas);
        result.getRecords().forEach(item -> {
            EnterPriseVo vo= new EnterPriseVo();
            EnterPrise  enterPrise = enterPriseService.getById(item.getEnterpriseId());
            if(enterPrise!=null){
                BeanUtils.copyProperties(enterPrise,vo);
                vo.setLicense(enterPrise.getLicense());
            }
            item.setEnterPriseVo(vo);
        });
        return  org.jeecg.common.api.vo.Result.ok(result);
    }
}
